package com.thh.ftp.controller;
import com.thh.ftp.pojo.SysFile;
import com.thh.ftp.response.Result;
import com.thh.ftp.response.StatusCode;
import com.thh.ftp.service.SysFileService;
import com.github.pagehelper.PageInfo;
import com.thh.ftp.util.Md5Utils;
import io.swagger.annotations.*;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;



/****
 * @Author:thh
 * @Description:
 * @Date 2019/3/6 10:15
 *****/
@Api(value = "SysFileController")
@RestController
@RequestMapping("/sysFile")
@CrossOrigin
public class SysFileController {

    @Autowired
    private SysFileService sysFileService;


    /**
     * 上传文件
     * @param multipartFile
     * @param request
     * @return
     */
    @PostMapping("upload")
    public SysFile updateAtt(@ApiParam(name = "MultipartFile对象", value = "上传文件", required = false) MultipartFile multipartFile, HttpServletRequest request) {
        Long userId = 1L;

       SysFile sysFile =  sysFileService.upload(multipartFile,request,userId);

        return sysFile;
    }


    /***
     * 根据ID返回文件流下载
     * @param id
     * @return
     */
    @ApiOperation(value = "返回文件流", notes = "根据ID返回文件流下载", tags = {"FileController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "String")
    @GetMapping("/download/{id}")
    public ResponseEntity<byte[]> download(HttpServletResponse response, @PathVariable String id) {
        ResponseEntity<byte[]> entity = sysFileService.download(response, id);
        return entity;
    }



    /***
     * 根据ID返回文件流展示
     * @param id
     * @return
     */
    @ApiOperation(value = "返回文件流", notes = "根据ID返回文件流展示", tags = {"FileController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "String")
    @GetMapping("/show/{id}")
    public ResponseEntity<byte[]> show(HttpServletResponse response, @PathVariable String id) {
        ResponseEntity<byte[]> entity = sysFileService.show(response, id);
        return entity;
    }



    /***
     * 根据附件主键ID删除附件
     * @param id
     * @return
     */
    @ApiOperation(value = "根据附件主键ID删除附件（保存半个月）",notes = "根据附件主键ID删除附件（保存半个月）",tags = {"FileController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Long")
    @GetMapping(value = "/deleteFake/{id}" )
    public Result deleteFake(@PathVariable String id){

        SysFile sysFile = sysFileService.deleteFake(id);
        return new Result(true,StatusCode.OK,"删除成功！",sysFile);


    }

    /***
     * 根据附件主键ID删除附件
     * @param id
     * @return
     */
    @ApiOperation(value = "根据附件主键ID删除附件",notes = "根据附件主键ID删除附件",tags = {"FileController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Long")
    @GetMapping(value = "/deleteReal/{id}" )
    public Result deleteReal(@PathVariable String id){

        SysFile sysFile = sysFileService.deleteReal(id);
        return new Result(true,StatusCode.OK,"删除成功！",sysFile);



    }


    /**
     * 查询列表
     * 一般只有管理员才能查所有
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/getList/{page}/{size}" )
    public Result getList(Map searchMap,@PathVariable("page") int page,@PathVariable("size") int size){

        PageInfo pageInfo = sysFileService.getList(page, size, searchMap);
        return new Result(true,StatusCode.OK,"查询成功！",pageInfo);
    }



    /**
     * 查询自己的列表
     * 一般只有管理员才能查所有
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/getMyList/{page}/{size}" )
    public Result getMyList(Map searchMap,@PathVariable("page") int page,@PathVariable("size") int size){

        PageInfo pageInfo = sysFileService.getMyList(page,size,searchMap,1L);
        return new Result(true,StatusCode.OK,"查询成功！",pageInfo);
    }

}
